<template>
  <div class="form-container">
    <el-form inline :model="form" label-width="90px">
      <el-form-item label="车辆使用费" prop="usageFee">
        <el-input size="default" v-model="form.usageFee" placeholder="车辆使用费 天/元"></el-input>
      </el-form-item>
      <el-form-item label="适用车辆类型" v-if="btnFlag" prop="vehicleTypeId">
        <el-select v-model="form.vehicleTypeId" placeholder="请选择车辆类型">
          <el-option v-for="item in TypeList" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <!-- <el-button type="primary" @click="handleReset(ruleForm as FormInstance)">重置</el-button> -->
      <el-button type="primary" v-if="btnFlag" @click="handleSearch">添加</el-button>
      <el-button type="primary" v-else @click="handleAdd">修改</el-button>
    </div>
  </div>
  <div class="wrap">
    <div v-for="item in List" :key="item.id">
      <div class="title">{{ item.typeName }}</div>
      <div>
        <el-radio-group v-model="radio1">
          <div class="radio-btn" @click="deviceSelect(it.deposit)" v-for="it in item.vehicleUsageFeeList" :key="it.id">
            <span @click="btnRemove(it.id)">x</span>
            <div>
              <el-radio-button
                @click.prevent="radioSelect(it, item)"
                :label="it.usageFee + '元'"
                :value="it.usageFee + item.typeName"
              ></el-radio-button>
            </div>
          </div>
        </el-radio-group>
      </div>
    </div>
  </div>
</template>
<script lang="tsx" setup name="deviceType">
import { onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { UsageFee } from "@/api/interface/city/car";
import { addUsageAPI, deleteUsageAPI, getUsageListAPI, updateUsageAPI } from "@/api/modules/city/carAPI";
import { CarType } from "@/api/interface/province/carManage";
import { getCarTypeListAPI } from "@/api/modules/province/carManageAPI";
const form = ref<UsageFee.AddParams>({
  province: JSON.parse(localStorage.getItem("userInfo") || "null").province,
  city: JSON.parse(localStorage.getItem("userInfo") || "null").city
});
let upId = ref<number>();
const radio1 = ref<string>("");
const radio2 = ref<string>("");
const btnFlag = ref<boolean>(true);
const List = ref<UsageFee.Datum[]>([]);
const radioSelect = (it, item) => {
  btnFlag.value = false;
  if (radio2.value == "" || radio1.value == "") {
    radio2.value = it.usageFee + item.typeName;
    radio1.value = it.usageFee + item.typeName;
    form.value.usageFee = it.usageFee;
    form.value.vehicleTypeId = item.id;
    upId.value = it.id;
  } else {
    if (radio2.value != it.usageFee + item.typeName) {
      radio1.value = it.usageFee + item.typeName;
      radio2.value = it.usageFee + item.typeName;
      form.value.usageFee = it.usageFee;
      form.value.vehicleTypeId = item.id;
      upId.value = it.id;
    } else {
      btnFlag.value = true;
      form.value = {
        province: JSON.parse(localStorage.getItem("userInfo") || "null").province,
        city: JSON.parse(localStorage.getItem("userInfo") || "null").city
      };
      radio1.value = "";
    }
  }
  console.log(btnFlag.value);
};
const btnRemove = (id: number): void => {
  ElMessageBox.confirm(`此操作将永久删除该设施类型, 是否继续?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      // contents.value.splice(contents.value.indexOf(item), 1);
      deleteUsage(id);
      getUsageList();
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
};

const deviceSelect = (item: number): void => {
  console.log(item);
  // form.value.deposit = Number(item);
};
const getUsageList = async () => {
  const { code, data } = await getUsageListAPI({
    province: JSON.parse(localStorage.getItem("userInfo") || "null").province,
    city: JSON.parse(localStorage.getItem("userInfo") || "null").city
  });
  if (code == "200" && data) {
    List.value = data;
  }
};
const deleteUsage = async (id: number) => {
  const { code } = await deleteUsageAPI(id);
  if (code == "200") {
    ElMessage.success("删除成功!");
    getUsageList();
  }
};
const addUsage = async (): Promise<void> => {
  const { code, msg } = await addUsageAPI(form.value);
  if (code == "200") {
    ElMessage.success("添加成功!");
    getUsageList();
    form.value = {
      province: JSON.parse(localStorage.getItem("userInfo") || "null").province,
      city: JSON.parse(localStorage.getItem("userInfo") || "null").city
    };
    btnFlag.value = true;
  } else {
    ElMessage.error(msg);
  }
};
const updateUsage = async (id: number, usageFee: number): Promise<void> => {
  const { code, msg } = await updateUsageAPI({ id, usageFee });
  if (code == "200") {
    ElMessage.success("修改成功!");
    getUsageList();
    form.value = {
      province: JSON.parse(localStorage.getItem("userInfo") || "null").province,
      city: JSON.parse(localStorage.getItem("userInfo") || "null").city
    };
    btnFlag.value = true;
  } else {
    ElMessage.error(msg);
  }
};
const handleSearch = () => {
  if (form.value.usageFee && form.value.vehicleTypeId) {
    addUsage();
  } else {
    ElMessage.error("请填写对应数据!");
  }
};
const handleAdd = () => {
  if (form.value.usageFee && form.value.vehicleTypeId) {
    updateUsage(upId.value as number, form.value.usageFee as number);
  } else {
    ElMessage.error("请填写对应数据!");
  }
};
let TypeList = ref<CarType.Row[]>([]);
const getCarTypeList = async () => {
  const { rows, code, msg } = await getCarTypeListAPI({
    pageNum: 1,
    pageSize: 10000,
    typeName: ""
  });
  if (code == "200" && rows) {
    TypeList.value = rows;
  } else {
    ElMessage.error(msg);
  }
};
onMounted(() => {
  getUsageList();
  getCarTypeList();
});
</script>
<style lang="scss" scoped>
.wrap {
  padding: 15px;
  background-color: white;
}
.form-container {
  padding: 10px 0;
  padding-right: 20px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
    ::v-deep .el-button > span {
      font-size: 13px;
    }
  }
  .el-form {
    display: inline-block;
    ::v-deep .el-form-item__label {
      font-size: 13px;
    }
    ::v-deep .el-input__inner {
      font-size: 13px;
    }
    ::v-deep .el-select_wapper {
      font-size: 13px;
    }
    .el-form-item {
      width: 280px;
      margin: 0;
      margin-right: 20px;
    }
  }
}
.btn {
  display: inline;
  float: right;
  margin-right: 10%;
}
.el-form-item {
  //   width: 320px;
  margin: 0;
}
.radio-btn {
  position: relative;
  display: inline-block;
  margin-right: 40px;
  background: whitesmoke;
  span {
    position: absolute;
    top: -8px;
    right: -8px;
    z-index: 1;
    display: inline-block;
    width: 16px;
    height: 16px;
    font-size: 12px;
    line-height: 14px;
    color: white;
    text-align: center;
    cursor: pointer;
    background-color: red;
    border-radius: 100%;
  }
  ::v-deep .el-radio-button__inner {
    width: 120px;
    padding: 13px 30px;
    font-size: 12px;
    background: lightgray;
  }
}
.select-container {
  box-sizing: border-box;
  height: 100px;
  padding-top: 30px;
  padding-left: 10px;

  // line-height: 100px;
  border: 1px solid #e2e2e2;
}
.title {
  margin-top: 20px;
  margin-bottom: 20px;
  font-size: 13px;
  color: gray;
}
</style>
